package com.spsy.pay.order;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {


    String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCwtqRQxR4X+pXcIU3NZTpmLF6jtIC+/8pUPVgrCKedCdvZylIlKn32fhXrUV/SQOeGmuOKEfNWgl6UgARUZeMr6cC/+Ar45WjudPjU1NndabrzEPkTjYJv4PnUQWU59pR5uKCJCv2L1WUlPnFgokyvTMOtvy5dFgjlIazytemoDjbDOph0Yq8UTmC5j4wxIM9+5e4ZQtod0JvMOGjU/JsRJFy9gTh8jucOr9SlEkZcspzDJtrjJho6RLU8XNiU9ZYoLuRcnyPy2slcIpJzUITh6wQEiQK+Aws23mJE5UquaqjaHj49efQyMZFX3CNzSqWpBpkpEbatHqB1DN7gKiH/AgMBAAECggEAVxwRb8dbiFJ8YSEBq0hFO2yP0n7+ICkTYedDeSMxDNZ5S6jSSYPiyAlrYK6iX5CjENV1aPBkVba6TNW2TKxVCaaW34q0TfE4gqOirbenFeAyyfMJ9TuCmwf530nFTodgtYfvit6iEbZFv9BCL3KR/rAGnbVqc3STOfAZwyMtZSuXjseeYukBbvHgIn+LAYFAa6cSoj3hR21x9GCU8zPkW+WvKqJHzK4jQ6oO8oD1aioRYQ6y0evn2goZAalbPhtKhYThdwZnjpvKKPc7K32EDwjR0F1/lJFKYYaND8BGQKDIM+FWUyMzMaAz2C0q0D8TpxOVFRI9Enr+JT58QhwI8QKBgQD999zEWkz1fdYrf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SGs8rXpqA42wzqYdGKvFE5guY+MMSDPfuXuGULaHdCbzDho1PybESRcvYE4fI7nDq/UpRJGXLKcwyba4yYaOkS1PFzYlPWWKC7kXJ8j8trJXCKSc1CE4esEBIkCvgMLNt5iROVKrmqo2h4+PXn0MjGRV9wjc0qlqQaZKRG2rR6gdQze4Coh/wIDAQAB";

    public static final String OUT_TRADE_NO = "400100234567919998";

    //生成支付订单接口
    public Map pay() throws AlipayApiException {
        //public Map pay(PayData payData) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();

        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");

        alipayConfig.setAppId("9021000142631254");

        alipayConfig.setPrivateKey(privateKey);

        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);

        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");

        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        /*model.setOutTradeNo(payData.getCode());
        model.setTotalAmount(payData.getMoney().toString());
        model.setSubject(payData.getName());*/

        model.setOutTradeNo(OUT_TRADE_NO);

        model.setTotalAmount("0.01");//元

        model.setSubject("测试产品");

        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        request.setBizModel(model);

        //后台配置文件不行
        //request.setNotifyUrl("https://71a6-61-181-211-208.ngrok-free.app/alipay");

        //回调地址
        request.setNotifyUrl("https://0209294dd2a4.ngrok-free.app/alipay/notify");




        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);

        System.out.println("==============================");
        System.out.println(response.getBody());
        System.out.println("===============================");

        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code",200);
            map.put("form",response.getBody());
        } else {
            map.put("code",400);
        }
        return map;
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000133628817");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);

        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }


     public static void main(String[] args) {
        try {
            ZhifubaoUtill utill = new ZhifubaoUtill();
            utill.pay();
            //utill.queryResult(OUT_TRADE_NO);
            Thread.sleep(1000);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
